package com.example.demo.service.impl;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

import com.example.demo.entity.User;
import com.example.demo.jpa.UserRepository;

@Service
public class UserServiceImpl implements UserDetailsService {

	private static final Logger log = LoggerFactory.getLogger(UserServiceImpl.class);
	@Bean
	BCryptPasswordEncoder getBCryptPasswordEncoder() {
		return new BCryptPasswordEncoder();
	}
	@Autowired
	UserRepository userRepository;
	@Override
	public UserDetails loadUserByUsername(String arg0) throws UsernameNotFoundException {
		log.info("\n 登录名 login:" + arg0);
		User user = userRepository.findByUserName(arg0);

		if (user == null) {
			throw new UsernameNotFoundException(String.format("没有该用户 '%s'.", arg0));
		} else {
			log.info("\n 用户权限author：" + user.getAuthorities());
			log.info("\n 登录名 login:" + user.getUserName());
			log.info("\n 登录密码 password:" + user.getPassword());
			return user;
		}
	}
}
